package com.ht.sps.nh.service.impl;

import com.ht.sps.nh.entity.TreeMenu;
import com.ht.sps.nh.service.TreeMenuService;
import com.ht.sps.service.impl.BaseServiceImpl;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: 王利
 * Date: 14-12-27
 * Time: 上午10:44
 */
@IocBean(fields = { "dao" }, name = "treeMenuService")
public class TreeMenuServiceImpl extends BaseServiceImpl<TreeMenu> implements TreeMenuService {

    private static final Logger log = LoggerFactory.getLogger(TreeMenuServiceImpl.class);
    public TreeMenuServiceImpl(){
        super();
    }
    public TreeMenuServiceImpl(Dao dao){
        super(dao);
    }

    @Override
     public List<TreeMenu> getTreeMenuList() {
        List<TreeMenu> list = query(Cnd.orderBy().asc("menu_level,id"), null);
        return list;
    }

    @Override
    public List<TreeMenu> getTreeMenuListByUserId(Long userId) {
        Sql sql = Sqls.create("select treeMenuId from $table where userId=@userId");
        sql.params().set("userId", userId);
        sql.vars().set("table", "SYSTEM_USER_PERMISSION");
        sql.setCallback(new SqlCallback() {
            public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                List<Long> list = new ArrayList();
                while (rs.next()){
                    Long l = rs.getLong("treeMenuId");
                    if (l != null) {
                        list.add(l);
                    }
                }
                return list;
            }
        });
        dao().execute(sql);
        List<Long> lists = sql.getList(Long.class);
        List<TreeMenu> list = new ArrayList<TreeMenu>();
        if(lists.size()>0){
           list = query(Cnd.where(Cnd.exps("id", "IN", lists)).or("id","=",1).or("parentId","in",lists), null);
        }else{
            list = query(Cnd.where(Cnd.exps("id","=",1)), null);
        }

        return list;
    }

    @Override
     public List<TreeMenu> getParentTreeMenuList() {
        List<TreeMenu> list = query(Cnd.where("menuLevel", "<", "3").and("model_count","=","0").and("id","<>","1"), null);
        return list;
    }

    @Override
    public boolean isLeaf(long id) {
        List<TreeMenu> list = query(Cnd.where("parentId", "=", id), null);
        if(list.size()>0){
            return false;
        }else{
            return true;
        }

    }

    @Override
    public void delTreeMenu(long id) {
        Sql sql = Sqls.create("delete T_TREE_MENU WHERE id=@id");
        sql.params().set("id", id);
        dao().execute(sql);
    }

    @Override
    public void init(){
        // 若必要的数据表不存在，则初始化数据库

            TreeMenu treeMenu = new TreeMenu();
            treeMenu.setId(1L);
            treeMenu.setMenuName("首页");
            treeMenu.setMenuLevel(1);
            treeMenu.setParentId(0);
            treeMenu.setParentId(0);
            insert(treeMenu);

    }
}
